const User = require('../../db/UserSchema');
const md5 = require('md5');

module.exports = async(req, res) => {
    let userData = await User.findOne({ "username": req.body.username });
    console.log(userData);
    if (!userData) {
        res.send('<script>alert("此用户不存在");location.href="/admin/login";</script>');
    } else {
        let userInfo = {
            username: req.body.username,
            password: md5(req.body.password)
        };
        var quRel = await User.find(userInfo);
        // 当查询结果长度为0时,[]
        console.log(quRel);
        if (quRel.length == 0) {
            res.send('<script>alert("密码错误");location.href="/admin/login";</script>');
        } else {
            // 设置session信息
            req.session.username = req.body.username;
            // 全局对象中设置一个信息--可以使ejs直接拿到数据
            req.app.locals.username = req.body.username;
            res.render('./admin/index.ejs');
        }
    }
};